package org.jeecg.common.modules.dicBeGoodAtNew.service.impl;

import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.modules.dicBeGoodAtNew.mapper.DicBeGoodAtNewMapper;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.modules.dicBeGoodAtNew.entity.DicBeGoodAtNew;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.IdGenerator;

/**
 * @Description: dic_be_good_at
 * @Author: jeecg-boot
 * @Date:   2021-08-13
 * @Version: V1.0
 */
@Service
public class DicBeGoodAtNewServiceImpl extends ServiceImpl<DicBeGoodAtNewMapper, org.jeecg.common.modules.dicBeGoodAtNew.entity.DicBeGoodAtNew> implements org.jeecg.common.modules.dicBeGoodAtNew.service.IDicBeGoodAtNewService {

	@Override
	public void addDicBeGoodAt(org.jeecg.common.modules.dicBeGoodAtNew.entity.DicBeGoodAtNew dicBeGoodAtNew) {
		if(oConvertUtils.isEmpty(dicBeGoodAtNew.getParentId())){
			dicBeGoodAtNew.setParentId(org.jeecg.common.modules.dicBeGoodAtNew.service.IDicBeGoodAtNewService.ROOT_PID_VALUE);
		}else{
			//如果当前节点父ID不为空 则设置父节点的hasChildren 为1
			org.jeecg.common.modules.dicBeGoodAtNew.entity.DicBeGoodAtNew parent = baseMapper.selectById(dicBeGoodAtNew.getParentId());
			if(parent!=null && !"1".equals(parent.getHasChild())){
				parent.setHasChild("1");
				baseMapper.updateById(parent);
			}
		}
		baseMapper.insert(dicBeGoodAtNew);
	}

	@Override
	public void updateDicBeGoodAt(org.jeecg.common.modules.dicBeGoodAtNew.entity.DicBeGoodAtNew dicBeGoodAtNew) {
		org.jeecg.common.modules.dicBeGoodAtNew.entity.DicBeGoodAtNew entity = this.getById(dicBeGoodAtNew.getId());
		if(entity==null) {
			throw new JeecgBootException("未找到对应实体");
		}
		Integer old_pid = entity.getParentId();
		Integer new_pid = dicBeGoodAtNew.getParentId();
		if(!old_pid.equals(new_pid)) {
			updateOldParentNode(old_pid+"");
			if(oConvertUtils.isEmpty(new_pid)){
				dicBeGoodAtNew.setParentId(org.jeecg.common.modules.dicBeGoodAtNew.service.IDicBeGoodAtNewService.ROOT_PID_VALUE);
			}
			if(!org.jeecg.common.modules.dicBeGoodAtNew.service.IDicBeGoodAtNewService.ROOT_PID_VALUE.equals(dicBeGoodAtNew.getParentId())) {
				baseMapper.updateTreeNodeStatus(dicBeGoodAtNew.getParentId()+"", org.jeecg.common.modules.dicBeGoodAtNew.service.IDicBeGoodAtNewService.HASCHILD+"");
			}
		}
		baseMapper.updateById(dicBeGoodAtNew);
	}

	@Override
	public void deleteDicBeGoodAt(String id) throws JeecgBootException {
		org.jeecg.common.modules.dicBeGoodAtNew.entity.DicBeGoodAtNew dicBeGoodAtNew = this.getById(id);
		if(dicBeGoodAtNew ==null) {
			throw new JeecgBootException("未找到对应实体");
		}
		updateOldParentNode(dicBeGoodAtNew.getParentId()+"");
		baseMapper.deleteById(id);
	}



	/**
	 * 根据所传pid查询旧的父级节点的子节点并修改相应状态值
	 * @param pid
	 */
	private void updateOldParentNode(String pid) {
		if(!org.jeecg.common.modules.dicBeGoodAtNew.service.IDicBeGoodAtNewService.ROOT_PID_VALUE.equals(pid)) {
			Integer count = baseMapper.selectCount(new QueryWrapper<org.jeecg.common.modules.dicBeGoodAtNew.entity.DicBeGoodAtNew>().eq("parent_id", pid));
			if(count==null || count<=1) {
				baseMapper.updateTreeNodeStatus(pid, org.jeecg.common.modules.dicBeGoodAtNew.service.IDicBeGoodAtNewService.NOCHILD+"");
			}
		}
	}

}
